package com.aviccxzx.smarthome.UserModule;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.entity.NewApkBean;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.aviccxzx.smarthome.update.BaseAndroid;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by joey on 2016/10/25.
 */
public class AboutAppActivity extends GosUserModuleBaseActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.aboutapp_lay_v19);
        } else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("关于APP");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.check_version).setOnClickListener(this);
        TextView app_version = (TextView) findViewById(R.id.app_version);
        app_version.setText("当前版本 " + getVersionName());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.check_version:
                checkApk();
                break;
        }
    }

    public void checkApk() {
        progressDialog.show();
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<NewApkBean> call = service.newestPack();
        call.enqueue(new Callback<NewApkBean>() {
            @Override
            public void onResponse(Call<NewApkBean> call, Response<NewApkBean> response) {
                if (response.body()!=null&&response.body().getData()!=null){
                    int versionSer = response.body().getData().getVersion();
                    String packname = response.body().getData().getPackName();
                    String packurl = response.body().getData().getPackUrl();
                    progressDialog.dismiss();
                    if (versionSer > getVersion()) {
                        //需要升级
                        BaseAndroid.checkUpdate(AboutAppActivity.this, versionSer,
                                packurl, "中航安居APP更新了", true);
                    } else {
                        //最新版本
                        showToast("已经是最新版本");
                    }
                }
            }

            @Override
            public void onFailure(Call<NewApkBean> call, Throwable t) {

            }
        });
    }

    //获取当前版本号
    private int getVersion() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //获取当前版本号
    private String getVersionName() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0.0";
        }
    }
}
